package rmaqlc31;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SungJukV1 extends JFrame {

	private JPanel contentPane;
	private JTextField name;
	private JTextField kor;
	private JTextField eng;
	private JTextField mat;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SungJukV1 frame = new SungJukV1();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SungJukV1() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel title = new JLabel(":: 성적 처리 프로그램 v1 ::");
		
		JLabel lblName = new JLabel("이  름");
		
		JLabel lblKor = new JLabel("국  어");
		
		JLabel lblEng = new JLabel("영  어");
		
		name = new JTextField();
		name.setColumns(10);
		
		kor = new JTextField();
		kor.setColumns(10);
		
		eng = new JTextField();
		eng.setColumns(10);
		
		JLabel lblMat = new JLabel("수  학");
		
		mat = new JTextField();
		mat.setColumns(10);
		
		JButton saveBtn = new JButton("입 력 완 료");
		saveBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//비지니스 로직 처리(총점,평균,학점 계산)
				int tot = Integer.parseInt(kor.getText())+Integer.parseInt(eng.getText())+Integer.parseInt(mat.getText());
				float avg = (float)tot/3;
				char grd = '가';
				switch((int)avg/10){
					case 10: case 9: grd = '수';break;
					case 8:			 grd = '우';break;
					case 7:			 grd = '미';break;
					case 6:			 grd = '양';
				}
				//결과 출력
				StringBuilder result = new StringBuilder();
				result.append("이름 : ").append(name.getText()).append("\n");
				result.append("국어 : ").append(kor.getText()).append("\n");
				result.append("영어 : ").append(eng.getText()).append("\n");
				result.append("수학 : ").append(mat.getText()).append("\n");
				result.append("총점 : ").append(tot).append("\n");
				result.append("평균 : ").append(String.format("%3.2f", avg)).append("\n");
				result.append("학점 : ").append(grd);
				JOptionPane.showMessageDialog(null, result);
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(138)
					.addComponent(title)
					.addContainerGap(142, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(73)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(lblName)
						.addComponent(lblKor)
						.addComponent(lblEng)
						.addComponent(lblMat))
					.addPreferredGap(ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addComponent(mat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(kor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(eng, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(61, Short.MAX_VALUE))
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGap(163)
					.addComponent(saveBtn)
					.addContainerGap(168, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(title)
					.addGap(38)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblName)
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblKor)
						.addComponent(kor, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblEng)
						.addComponent(eng, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblMat)
						.addComponent(mat, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addComponent(saveBtn)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		contentPane.setLayout(gl_contentPane);
	}
}
